--[[-------------------------------------------------------------------------
  Copyright (c) 2006-2007, Trond A Ekseth
  All rights reserved.

  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions are
  met:

      * Redistributions of source code must retain the above copyright
        notice, this list of conditions and the following disclaimer.
      * Redistributions in binary form must reproduce the above
        copyright notice, this list of conditions and the following
        disclaimer in the documentation and/or other materials provided
        with the distribution.
      * Neither the name of oApplication nor the names of its
        contributors may be used to endorse or promote products derived
        from this software without specific prior written permission.

  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
---------------------------------------------------------------------------]]

local frame = CreateFrame("Button", "oApplication", UIParent, "SecureActionButtonTemplate")
local snatch = CreateFrame("Frame", nil, GameTooltip)
local model = CreateFrame("Model", nil, frame)
local macro = "/stopcasting\n/use %s\n/use [button:2] 17; 16"

-- From PeriodicTable
local GetID = function(item)
	if type(item) == "number" then return item
	elseif type(item) == "string" then
		local _, _, id = string.find(item, "item:(%d+):%d+:%d+:%d+")
		if id then return tonumber(id) end
	end
end
local S = function() return not TradeFrame:IsShown() and not BankFrame:IsShown() and not MerchantFrame:IsShown() and not InCombatLockdown() and IsAltKeyDown() end
local E = "23529 28421 23123 23122 18262 21835 6533 6811 31535 6532 20748 20749 2863 3240 3775 3776 2892 2893 8984 8985 20844 22053 22054 12404 12643 23528 28420 7307 3829 23576 25521 2871 3241 6947 6949 6950 8926 8927 8928 21927 20747 23575 23559 20746 5237 6951 9186 20745 20744 6530 2862 3239 3824 6529 7964 7965 22521 22522 20750 10918 10920 10921 10922 22055 "
local OnShow = function()
	local name, id = GameTooltip:GetItem()
	frame:ClearAllPoints()

	if(name and id) then
		id = GetID(id)
		if(id and E:match(id.."%s") and S()) then
			frame:SetAttribute("macrotext", macro:format(name))
			frame:SetAllPoints(GetMouseFocus())
			frame:Show()
		end
	end
end

frame:SetFrameStrata"DIALOG"
frame:RegisterForClicks("LeftButtonUp", "RightButtonUp")
frame:SetScript("OnLeave", function(self) self:Hide() end)
frame:SetAttribute("*type*", "macro")

frame:SetScript("OnEvent", function(self, event, key, state)
	if(key == "ALT") then
		if(state == 1) then OnShow()
		else self:Hide() end
	end
end)
frame:RegisterEvent("MODIFIER_STATE_CHANGED")

model:SetModel"Interface\\Buttons\\UI-AutoCastButton.mdx"
model:SetAllPoints(frame)
model:SetSequence(0)
model:SetSequenceTime(0, 0)
model:SetScale(1.3)

snatch:SetScript("OnShow", OnShow)
